--local print = LibStub("AceConsole-3.0").Print

evl_Reminders = CreateFrame("Frame", nil, UIParent)
local UPDATE_INTERVAL = 1
local ICON_PATH = "Interface\\Icons\\"
local DEFAULT_ICON = "Temp"

local lastUpdate = 0
local reminders = {}
local menuFrame = CreateFrame("Frame", "evl_RemindersMenu", UIParent, "UIDropDownMenuTemplate")
local menu = {}

local supressReminder = function(self)
	--print("Supressing reminder: " .. self.name)
	
	self.active = false
	self.reactivateTime = GetTime() + 300
end

local disableReminder = function(self)
	--print("Disabling reminder: " .. self.name)
	
	self.active = false
end

local onUpdate = function(self, elapsed)
	lastUpdate = lastUpdate + elapsed
	
	if lastUpdate > UPDATE_INTERVAL then
		lastUpdate = 0
		
		for _, reminder in ipairs(reminders) do
			if not reminder.active and (reminder.reactivateTime > 0 and GetTime() >= reminder.reactivateTime) then
				--print("Reactivating reminder: " .. reminder.name)
				
				reminder.active = true
				reminder.reactivateTime = 0
			end
		
			local result = reminder.active and reminder.callback()
			
			if result then
				--print("Showing reminder: " .. reminder.name)
				reminder:Show()
			else
				--print("Hiding reminder: " .. reminder.name)
				reminder:Hide()
			end
		end

		self:UpdatePositions()
	end
end

local onEvent = function(self, event)
	if event == "PLAYER_REGEN_DISABLED" then
		self:SetScript("OnUpdate", nil)
	else
		self:SetScript("OnUpdate", onUpdate)
	end
end

local onEnter = function(self)
	--print("Showing tooltip for button: " .. self.name)
	GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
	GameTooltip:SetWidth(250)
	GameTooltip:AddLine(self.name)
	
	if self.tooltip then
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine(self.tooltip)
	else
		local left = self:GetAttribute("item1")
		if left then
			GameTooltip:AddLine(" ")
			GameTooltip:AddLine("Left-click to use " .. left)
		end
		
		left = self:GetAttribute("spell1")		
		if left then
			GameTooltip:AddLine(" ")
			GameTooltip:AddLine("Left-click to cast " .. left)
		end		
		
		local right = self:GetAttribute("item2")	
		if right then
			if not left then
				GameTooltip:AddLine(" ")
			end
			
			GameTooltip:AddLine("Right-click to use " .. right)
		end
		
		right = self:GetAttribute("spell2")	
		if right then
			if not left then
				GameTooltip:AddLine(" ")
			end
			
			GameTooltip:AddLine("Right-click to cast " .. right)
		end
	end
	
	GameTooltip:Show()
end

local onLeave = function(self)
	GameTooltip:Hide()
end

local onClick = function(self)
	menu = {
		{text = self.name, isTitle = true},
		{text = "Supress for 5 minutes", func = supressReminder, arg1 = self},
		{text = "Disable for this session", func = disableReminder, arg1 = self}
	}
	
	EasyMenu(menu, menuFrame, "cursor", nil, nil, "MENU")
end


-- Utility functions
function evl_Reminders:IsInPartyWith(class)
	local numMembers = GetNumPartyMembers()
	
	if numMembers then
		for i = 1, numMembers do
			if select(2, UnitClass("party" .. i)) == class then
				return true
			end
		end
	end

	return false
end

function evl_Reminders:IsBigWigsModuleActive(name)
	local bw = _G.BigWigs
	
	return bw and bw:IsModuleActive(name)
end

function evl_Reminders:PlayerHasBuff(name)
	for i = 1, 16 do
		local buffName = GetPlayerBuffName(i)
		
		if not buffName then
			break
		elseif buffName == name then
			return true
		end
	end

	return false
end

function evl_Reminders:AddReminder(name, callback, icon, attributes, tooltip)
	local buttonName = "ReminderButton" .. #reminders
	local frame = CreateFrame("Button", buttonName, self, "SecureActionButtonTemplate, ActionButtonTemplate")	
	--print("Creating reminder:" .. name .. " button: " .. frame:GetName())
	
	frame.name = name
	frame.callback = callback	
	frame.attributes = attributes
	frame.tooltip = tooltip
	frame.active = true
	frame.reactivateTime = 0

	local texture = frame:CreateTexture(nil, "BACKGROUND")
	texture:SetAllPoints(frame)
	texture:SetTexture(ICON_PATH .. (icon or DEFAULT_ICON))
	
	_G[frame:GetName() .. "Icon"]:SetTexture(texture)

	frame:RegisterForClicks("AnyUp")
	frame:SetScript("OnEnter", onEnter)
	frame:SetScript("OnLeave", onLeave)
	
	if attributes then
		for key, value in pairs(attributes) do
			frame:SetAttribute(key, value)
		end
	end
	
	frame:SetAttribute("alt-type*", "showmenu")
	frame.showmenu = onClick
	
	table.insert(reminders, frame)
	return reminder	
end

function evl_Reminders:UpdatePositions()
	local previousReminder = nil
	
	for _, reminder in ipairs(reminders) do
		if reminder:IsVisible() then
			reminder:ClearAllPoints()
			
			if previousReminder then
				--print("Placing reminder: " .. reminder.name .. " next to: " .. previousReminder.name)
				reminder:SetPoint("LEFT", previousReminder, "RIGHT", 5, 0)
			else
				--print("Placing first reminder: " .. reminder.name)
				reminder:SetPoint("TOPLEFT", self)
			end
			
			previousReminder = reminder
		end
	end
end

evl_Reminders:SetWidth(36)
evl_Reminders:SetHeight(36)
evl_Reminders:SetPoint("CENTER", UIParent, 300, 0)
evl_Reminders:SetScript("OnEvent", onEvent)
evl_Reminders:RegisterEvent("PLAYER_ENTERING_WORLD")
evl_Reminders:RegisterEvent("PLAYER_REGEN_ENABLED")
evl_Reminders:RegisterEvent("PLAYER_REGEN_DISABLED")
